﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace ThoiTrangT2B.Admin
{
    public partial class Promotion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ThemMoiBanner.Visible = false;
                LoadListBanner();
            }
        }

        protected void LoadListBanner()
        {
            string sql = "SELECT * FROM dbo.tblWebQuangCao";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            grvData.DataSource = dt;
            grvData.DataBind();

        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            ThemMoiBanner.Visible = true;
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            ThemMoiAnhBanner();
            LoadListBanner();
            ThemMoiBanner.Visible = false;
        }

        protected void ThemMoiAnhBanner()
        {
            string sql = "Insert into dbo.tblWebQuangCao values(@AttachFile,@IsUse,@UserID,GETDATE(),GETDATE())";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@AttachFile", SqlDbType.NVarChar, ParameterDirection.Input, UploadFile()));
            arrParams.Add(SqlHelper.CreateParameterObject("@IsUse", SqlDbType.Bit, ParameterDirection.Input, rbtActive.Checked));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, 1));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        protected void DeleteAnhBanner()
        {

        }

        protected void SuaAnhBanner(bool Active, int ID)
        {
            string sql = "UPDATE dbo.tblWebQuangCao SET IsUse = @IsUse,UserID = @UserID,UpdateDate = GETDATE() WHERE ID = @ID";

            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@IsUse", SqlDbType.Bit, ParameterDirection.Input, Active));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, 1));
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;

            if ((sCommandName == ThoiTrangT2B.MyConst.Action.Approve))
            {
                List<SqlParameter> arrParams = new List<SqlParameter>();
                RadioButton rbtSuDung = (RadioButton)grvData.Rows[grvData.EditIndex].Cells[1].Controls[1];
                SuaAnhBanner(rbtSuDung.Checked, Convert.ToInt32(sID));
            }
            if ((sCommandName == ThoiTrangT2B.MyConst.Action.Delete))
            {
                string sql = "Select AttachFile from tblWebQuangCao where ID = " + sID;
                DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
                string AttachFile = dt.Rows[0]["AttachFile"].ToString();
                sql = "DELETE FROM dbo.tblWebQuangCao WHERE ID = " + sID;
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
                try
                {
                    File.Delete(Server.MapPath("../" + AttachFile));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.StackTrace);
                }


            }
            grvData.EditIndex = -1;
            LoadListBanner();
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvData.EditIndex = e.NewEditIndex;
            LoadListBanner();
        }

        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            LoadListBanner();
        }

        protected void grvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            grvData.DeleteRow(e.RowIndex);
            grvData.DataBind();
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            ThemMoiBanner.Visible = false;
        }

        private string UploadFile()
        {
            try
            {
                if (ImgUploadBanner.HasFile)
                {
                    string sFolderPath = Server.MapPath(@"../cp/upload/item");
                    HttpPostedFile myFile = ImgUploadBanner.PostedFile;
                    string sFileName = myFile.FileName;
                    string[] arrFileName = sFileName.Split('.');
                    arrFileName[0] = arrFileName[0].Replace(' ','_')+DateTime.Now.ToString("ddMMyyhhssmm");
                    sFileName = arrFileName[0] + "." + arrFileName[1];
                    myFile.SaveAs(string.Format(@"{0}\{1}", sFolderPath, sFileName));
                    return "cp/upload/item/" + sFileName;
                }
                else
                {
                    return "";
                }
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
